package com.gsit.misn.utils;

import com.gsit.misn.common.Gender;
import com.gsit.misn.common.MaritalStatus;
import com.gsit.misn.common.MisnExcuterType;
import com.gsit.misn.domain.Patient;
import com.thoughtworks.selenium.DefaultSelenium;

public class PatientUtil {
	
	public static void setPatientIntoSelenium(DefaultSelenium selenium,Patient patient,MisnExcuterType excuterType  ){
		
		selenium.type("pid",patient.getPatientID() );
		selenium.type("pname",patient.getPatientName() );
		selenium.type("dname",patient.getDoctorName() );
		selenium.type("rname",patient.getRelativeName() );
		selenium.type("address",patient.getAddress() );
		selenium.type("city",patient.getCity() );
		selenium.type("state",patient.getState() );
		selenium.type("zip",patient.getZip() );
		selenium.type("country",patient.getCountry() );
		selenium.type("phone",patient.getPhone() );
		selenium.type("email",patient.getEmail() );			
		selenium.type("height",patient.getHeight() );
		selenium.type("occ",patient.getOccupation() );
		selenium.type("weight", patient.getWeight());
		
		boolean isElm = selenium.isElementPresent("total");
		if(isElm){
			selenium.type("total",  getTotal(selenium.getBodyText()));
		}
		
		if(MisnExcuterType.CREATE == excuterType ){
			setPatientIntoSeleniumForCreate(selenium, patient);
		}else if(MisnExcuterType.UPDATE == excuterType ){
			setPatientIntoSeleniumForUpdate(selenium, patient);
		}else{
			
		}
		
		
		selenium.type("dob",patient.getDateofBirth() );
		
		
	}
	
	
	private static String getTotal(String body){
		int totla = body.length();
		int trm = body.lastIndexOf("Enter the total value of");
		
		String newValue = body.substring(trm+25, totla).trim();
		
		int totlatNo = Integer.parseInt(""+newValue.charAt(0)) + Integer.parseInt(""+newValue.charAt(4));
		System.out.println("The totla is "+totlatNo);
		return ""+totlatNo;
		
	}
	
	private static void setPatientIntoSeleniumForCreate(DefaultSelenium selenium,Patient patient){
		if("Male".equalsIgnoreCase(patient.getGender())){
			selenium.check("name=gender value=Male");
		}else if("Female".equalsIgnoreCase(patient.getGender())){
			selenium.check("name=gender value=Female");
		}else{
			System.out.println("No Gender is not found");
		}
		
		if("Single".equalsIgnoreCase(patient.getMaritalStatus())){
			selenium.check("name=mstatus value=Single");
		}else if("Married".equalsIgnoreCase(patient.getMaritalStatus())){
			selenium.check("name=mstatus value=Married");
		}else{
			System.out.println("No MaritalStatus is not found");
		}
	}
	
	//TODO need to implement
	private static void setPatientIntoSeleniumForUpdate(DefaultSelenium selenium,Patient patient){
		selenium.type("mstatus",MaritalStatus.fromCode(patient.getMaritalStatus()).toCode() );
		selenium.type("gender", Gender.fromCode(patient.getGender()).toCode());
	}
	
	


}
